]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/Tests/Unit Tests/DNSSEC Unit Tests/Utility/BaseNEncodingDecodingTest.m
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / Tests / Unit Tests / DNSSEC Unit Tests / Utility / BaseNEncodingDecodingTest.m
1 //
2 // BaseNEncodingDecodingTest.m
3 // Tests
4 //
5 // Copyright (c) 2020 Apple Inc. All rights reserved.
6 //
7
8 #import <XCTest/XCTest.h>
9 #include "base_n.h"
10 #include "mDNSEmbeddedAPI.h"
11 #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
12
13 @interface BaseNEncodingDecodingTest : XCTestCase
14
15 @end
16
17 @implementation BaseNEncodingDecodingTest
18
19 char * encoded_str = NULL;
20
21 - (void) testBase64Encoding {
22 unsigned char data_input[1024];
23 char * test_case_ptr;
24 char * answer_ptr;
25 char * test_cases[] = {
26 "", "f", "fo", "foo", "foob", "fooba", "foobar"
27 };
28 char * answers[] = {
29 "", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"
30 };
31
32 XCTAssertEqual(sizeof(test_cases), sizeof(answers));
33
34 for (int i = 0, limit = sizeof(answers) / sizeof(char *); i < limit; i++) {
35 test_case_ptr = test_cases[i];
36 answer_ptr = answers[i];
37 strlcpy((char *)data_input, test_case_ptr, sizeof(data_input));
38
39 XCTAssertEqual(strlen(answer_ptr), get_base_n_encoded_str_length(DNSSEC_BASE_64, strlen(test_case_ptr)));
40
41 encoded_str = base_n_encode(DNSSEC_BASE_64, data_input, strlen(test_case_ptr));
42 XCTAssertTrue(encoded_str != NULL);
43 XCTAssertTrue(strcmp(encoded_str, answer_ptr) == 0, "i: %d, input: %s, encoded_str: %s, answer_ptr: %s",
44 i, test_case_ptr, encoded_str, answer_ptr);
45
46 free(encoded_str);
47 encoded_str = NULL;
48 }
49 }
50
51 - (void) testBase32HexEncoding {
52 unsigned char data_input[1024];
53 char * test_case_ptr;
54 char * answer_ptr;
55 char * test_cases[] = {
56 "", "f", "fo", "foo", "foob", "fooba", "foobar"
57 };
58 char * answers[] = {
59 "", "CO======", "CPNG====", "CPNMU===", "CPNMUOG=", "CPNMUOJ1", "CPNMUOJ1E8======"
60 };
61
62 XCTAssertEqual(sizeof(test_cases), sizeof(answers));
63
64 for (int i = 0, limit = sizeof(answers) / sizeof(char *); i < limit; i++) {
65 test_case_ptr = test_cases[i];
66 answer_ptr = answers[i];
67 strlcpy((char *)data_input, test_case_ptr, sizeof(data_input));
68
69 XCTAssertEqual(strlen(answer_ptr), get_base_n_encoded_str_length(DNSSEC_BASE_32_HEX, strlen(test_case_ptr)));
70
71 encoded_str = base_n_encode(DNSSEC_BASE_32_HEX, data_input, strlen(test_case_ptr));
72 XCTAssertTrue(encoded_str != NULL);
73 XCTAssertTrue(strcmp(encoded_str, answer_ptr) == 0, "i: %d, input: %s, encoded_str: %s, answer_ptr: %s",
74 i, test_case_ptr, encoded_str, answer_ptr);
75
76 free(encoded_str);
77 encoded_str = NULL;
78 }
79 }
80
81 - (void) tearDown {
82 if (encoded_str != NULL) {
83 free(encoded_str);
84 }
85 }
86
87 @end
88 #endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)